package com.jd.jstable.strategy.redis;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

/**
 * Created by chenyipu on 2018/6/6.
 */
@Component("jstable_______JedisPool")
public class JStableJedisPoolFactoryBean implements FactoryBean<JedisPool> {

    @Value("${jstable.redis.pool.maxTotal}")
    int maxActive;
    @Value("${jstable.redis.pool.maxIdle}")
    int maxIdle;
    @Value("${jstable.redis.host}")
    String host;
    @Value("${jstable.redis.port}")
    Integer port;

    @Override
    public JedisPool getObject() throws Exception {
        GenericObjectPoolConfig jedisPoolConfig = new GenericObjectPoolConfig();
        jedisPoolConfig.setMaxTotal(maxActive);
        jedisPoolConfig.setMaxIdle(maxIdle);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port);
        return jedisPool;
    }

    @Override
    public Class<?> getObjectType() {
        return JedisPool.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
